<!doctype html>
<html>
<head>
  <script src="./js/config.js"></script>
  <meta charset="utf-8">
  <title>报警设置</title>
  <style>
    body{font-family:Arial,Helvetica,sans-serif;margin:40px}
    .nav{margin-bottom:20px}
    .nav a{margin-right:15px;font-weight:bold}
    input{margin-right:8px}
  </style>
</head>
<body>
<div class="nav">
  <a href="index.html">🏠 返回首页</a>
</div>
<h2>报警时间设置</h2>
<form id="form">
  每天几点：<input type="time" name="time" id="time" required><br>
  周几报警（多选）：<br>
  <label><input type="checkbox" name="days" value="1"> 周一</label>
  <label><input type="checkbox" name="days" value="2"> 周二</label>
  <label><input type="checkbox" name="days" value="3"> 周三</label>
  <label><input type="checkbox" name="days" value="4"> 周四</label>
  <label><input type="checkbox" name="days" value="5"> 周五</label>
  <label><input type="checkbox" name="days" value="6"> 周六</label>
  <label><input type="checkbox" name="days" value="7"> 周日</label><br>
  <button>保存</button>
</form>

<script>

async function load(){
  const cfg = await fetch(BASE+'/api/schedule').then(r=>r.json());
  document.getElementById('time').value = cfg.time;
  document.querySelectorAll('input[name=days]').forEach(el=>{
    el.checked = cfg.days.includes(parseInt(el.value)) || (el.value==7 && cfg.days.includes(0));
  });
}

document.getElementById('form').onsubmit = async (e)=>{
  e.preventDefault();
  const fd = new FormData(e.target);
  const res = await fetch(BASE+'/api/schedule', {method:'POST', body: fd});
  alert(await res.text());
};

load();
</script>
</body>
</html>
